using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0167 //2008.12.15 //Begin
namespace InfoJetSoft.Service.EscapeRule
{
	/// <summary>
	/// Summary description for DataFlowImportParameter.
	/// </summary>
	class ImportParameter
	{
		private string _match = null;
		private bool _onNew = false;
		private bool _onOpen = false;
		private bool _onPostback = false;
		private string _source = null;
		private string _sourceScope = null;
		private string _sourceKey = null;

		public static ImportParameter parse( string match, string message )
		{
			int start = message.IndexOf( "{" );
			int end = message.LastIndexOf( "}" );
			string content = message.Substring( start + 1, end - start - 1 );
            //CID 0622 //2010.07.01 //Begin
            return parseContent( match, content );
            //CID 0622 //2010.07.01 //End
		}

        //CID 0622 //2010.07.01 //Begin
        public static ImportParameter parseContent(string match, string content)
        {
            ImportParameter importParameter = new ImportParameter();
            importParameter._match = match;

            string[] items = content.Split(new char[] { ';' });
            foreach (string curItem in items)
            {
                string item = curItem.Trim();
                if (item.Length > 0)
                {
                    string[] pair = item.Split(new char[] { ':' });
                    string key = pair[0];
                    string value = pair[1];
                    if (key.Equals("Event"))
                    {
                        string[] events = value.Split(new char[] { ',' });
                        foreach (string curEvent in events)
                        {
                            string evn = curEvent.Trim();
                            if (evn.Equals("OnNew"))
                            {
                                importParameter._onNew = true;
                            }
                            if (evn.Equals("OnOpen"))
                            {
                                importParameter._onOpen = true;
                            }
                            if (evn.Equals("OnPostback"))
                            {
                                importParameter._onPostback = true;
                            }
                        }
                    }
                    if (key.Equals("Source"))
                    {
                        importParameter._source = value.Trim();
                    }
                    if (key.Equals("SourceScope"))
                    {
                        importParameter._sourceScope = value.Trim();
                    }
                    if (key.Equals("SourceKey"))
                    {
                        importParameter._sourceKey = value.Trim();
                    }
                }
            }
            return importParameter;
        }
        //CID 0622 //2010.07.01 //End

		public static void ExecuteOnNew( Form form )
		{
			if( form.Manifest.ImportParameterList != null )
			{
				foreach( ImportParameter importParameter in form.Manifest.ImportParameterList )
				{
					if( importParameter._onNew )
					{
						Execute( form, importParameter );
					}
				}
			}
		}

		public static void ExecuteOnOpen( Form form )
		{
			if( form.Manifest.ImportParameterList != null )
			{
				foreach( ImportParameter importParameter in form.Manifest.ImportParameterList )
				{
					if( importParameter._onOpen )
					{
						Execute( form, importParameter );
					}
				}
			}
		}

		public static void ExecuteOnPostback( Form form )
		{
			if( form.Manifest.ImportParameterList != null )
			{
				foreach( ImportParameter importParameter in form.Manifest.ImportParameterList )
				{
					if( importParameter._onPostback )
					{
						//CID 0474 //2009.12.24 //Begin
						form.Logger.LogUpdate(null);
						//CID 0474 //2009.12.24 //End
						Execute( form, importParameter );
					}
				}
			}
		}

		private static void Execute( Form form, ImportParameter importParameter )
		{
			XmlNodeList nodeList = DOMUtil.selectNodes( form.XmlDoc, importParameter._match, 0, form.XmlNsResolver );
			foreach( XmlNode xmlNode in nodeList )
			{
				if( importParameter._source != null && importParameter._source.Equals( "NewGuid" ) )
				{
					DOMUtil.setNodeValue( xmlNode, Guid.NewGuid().ToString() );
				}
				if( importParameter._sourceScope != null && importParameter._sourceKey != null )
				{
					if( importParameter._sourceScope.Equals( "Header" ) )
					{
						string header = form.HttpContext.Request.Headers[ importParameter._sourceKey ];
						if( header != null )
						{
							DOMUtil.setNodeValue( xmlNode, header );
						}
					}
					//CID 0184 //2009.01.11 //Begin
					else if( importParameter._sourceScope.Equals( "QueryString" ) )
					{
						string queryString = form.HttpContext.Request.QueryString[ importParameter._sourceKey ];
						if( queryString != null )
						{
							DOMUtil.setNodeValue( xmlNode, queryString );
						}
					}
					else if( importParameter._sourceScope.Equals( "Form" ) )
					{
						string formField = form.HttpContext.Request.Form[ importParameter._sourceKey ];
						if( formField != null )
						{
							DOMUtil.setNodeValue( xmlNode, formField );
						}
					}
					else if( importParameter._sourceScope.Equals( "Cookies" ) )
					{
						System.Web.HttpCookie cookie = form.HttpContext.Request.Cookies[ importParameter._sourceKey ];
						if( cookie != null && cookie.Value != null )
						{
							DOMUtil.setNodeValue( xmlNode, cookie.Value );
						}
					}
					else if( importParameter._sourceScope.Equals( "ServerVariables" ) )
					{
						string serverVariable = form.HttpContext.Request.ServerVariables[ importParameter._sourceKey ];
						if( serverVariable != null )
						{
							DOMUtil.setNodeValue( xmlNode, serverVariable );
						}
					}
					//CID 0184 //2009.01.11 //End
					else if( importParameter._sourceScope.Equals( "Request" ) )
					{
						string param = form.HttpContext.Request[ importParameter._sourceKey ];
						if( param != null )
						{
							DOMUtil.setNodeValue( xmlNode, param );
						}
					}
					else if( importParameter._sourceScope.Equals( "Context" ) )
					{
						object item = form.HttpContext.Items[ importParameter._sourceKey ];
						if( item != null )
						{
							DOMUtil.setNodeValue( xmlNode, item.ToString() );
						}
					}
					else if( importParameter._sourceScope.Equals( "Session" ) )
					{
						object param = form.HttpContext.Session[ importParameter._sourceKey ];
						if( param != null )
						{
							DOMUtil.setNodeValue( xmlNode, param.ToString() );
						}
					}
					else if( importParameter._sourceScope.Equals( "Application" ) )
					{
						object param = form.HttpContext.Application[ importParameter._sourceKey ];
						if( param != null )
						{
							DOMUtil.setNodeValue( xmlNode, param.ToString() );
						}
					}
				}
			}
		}

	}
}
//CID 0167 //2008.12.15 //End
